arrayProto = Array.prototype
const arrayMethods = Object.create(arrayProto);
[
    'push',
    'shift',
    'unshift',
    'pop',
    'splice',
    'reverse',
    'sort',
].forEach(function(method) {
    const original = arrayProto[method]
    Object.defineProperty(arrayMethods, method, {
        value: function mutator (...args) {
            return original.apply(this, args)
        }
    })
})

function defineReactive(obj, key, val) {
    observe(val);
    Object.defineProperty(obj, key, {
        get() {
            console.log(`get ${obj} : ${key}`);
            return val;
        },
        set(v) {
            if(v !== val) {
                val = v;
                console.log(`set ${obj} : ${key}`);
                if(typeof v === 'object'){
                    observe(v);
                }
            }
        }
    })
}

function observe(obj){
    if(typeof obj !== 'object' || obj == null) return;
    Object.keys(obj).forEach(key => {
        defineReactive(obj, key, obj[key]);
    })
}

let obj = {a: 1, b: 2, c: 3, d: [1,2,3]};
observe(obj);
console.log(obj.d)
obj.d.unshift(5)
console.log(obj.d)
obj.d[0] = 11
obj.d[3] = 12

// obj.d[0] = 10

// obj.d[0] = 10;
// console.log(obj.d[0]);

//Object.defineProperty对数组无效
